package com.china08.yxyapi.api.comm.userInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTask;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class UpdateUserInfoController extends YxyApiBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpdateUserInfoController.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation(value = "获得格式")
    @RequestMapping(value = "/api_yw/comm/update/user/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String find(@RequestBody UserInfo userInfo) {

        // 学生部分
        if ("0".equals(userInfo.getType())) {
            LOGGER.info("学生部分。。。。。。。。。。。。。。。。");
            // 教师
        } else if ("2".equals(userInfo.getType())) {
            LOGGER.info("教师家长部分。。。。。。。。。。。。。。。。");
            // 2017-12-27 json
            // HomeworkTask
            Query query = new Query(Criteria.where("publishUserId").is(userInfo.getId()));
            Update update = new Update();
            update.set("publishUserNick", userInfo.getNickname());
            mongoTemplate.updateMulti(query, update, HomeworkTask.class);
            // HomeworkTaskClass
            Query query1 = new Query(Criteria.where("publishUserId").is(userInfo.getId()));
            Update update1 = new Update();
            update1.set("publishUserNick", userInfo.getNickname());
            mongoTemplate.updateMulti(query1, update1, HomeworkTaskClass.class);
            // HomeworkTaskReportQues
            Query query2 = new Query(Criteria.where("publishUserId").is(userInfo.getId()));
            Update update2 = new Update();
            update2.set("publishUserNick", userInfo.getNickname());
            mongoTemplate.updateMulti(query2, update2, HomeworkTaskReportQues.class);
        }
        return "ok";
    }

    protected static class UserInfo {
        // 0 学生 2 家长教师
        public String type;
        public String id;
        public String avatar;
        public String nickname;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

    }

}
